﻿@model BackEnd.Models.PrivilegeModel
@{
    ViewBag.Title = "Privileges Management";
}

<h2>Privileges Management</h2>
@using (Html.BeginForm())
{ 
    @Html.ValidationSummary(true, "Grant privileges to user gorups was unsuccessful. Please correct the errors and try again.", new { @class="error_box" }) 
                        
    if (ViewData["ErrorMessage"] != null)
    {
    @Html.Label(ViewData["ErrorMessage"].ToString())
    }
    <table>
        <tr>
            <td>
                <table>
                    <tr>
                        <td colspan="3">SELECT USER GROUPS TO GRANT PRIVILEGES</td>
                    </tr>
                    <tr>
                        <td>@Html.ListBox("listbox", new MultiSelectList(Model.ListUserGroups, "UserGroupID", "Code"), new { id = "lbxUserGroups", @title = "Unselected user groups", style="width:250px" })</td>
                        <td>
                            <input type="button" value=">>" id="ibtnAddUserGroup" title="Add" /><br />
                            <input type="button" value="<<" id="ibtnRemoveUserGroup" title="Remove" /></td>
                        <td>@Html.ListBoxFor(m => m.SelectedUserGroupIDs, new MultiSelectList(Enumerable.Empty<SelectListItem>(), "UserGroupID", "Code"), new { id = "lbxSelectedUserGroups", @title = "Selected user groups", style="width:250px" })
                            @Html.ValidationMessageFor(m => m.SelectedUserGroupIDs)

                        </td>

                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td>
                <table>
                    <tr>
                        <td colspan="3">SELECT USERS ADD TO GROUPS ABOVE</td>
                    </tr>
                    <tr>
                        <td>@Html.ListBox("listbox", new MultiSelectList(Model.ListUsers, "UserID", "UserName"), new { id = "lbxUsers", @title = "Unselected users", style="width:250px"})</td>
                        <td>
                            <input type="button" value=">>" id="ibtnAddUsers" title="Add" /><br />
                            <input type="button" value="<<" id="ibtnRemoveUsers" title="Remove" /></td>
                        <td>@Html.ListBoxFor(m => m.SelectedUserIDs, new MultiSelectList(Enumerable.Empty<SelectListItem>(), "UserID", "UserName"), new { id = "lbxSelectedUsers", @title = "Selected users", style="width:250px" })</td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td>
                <table>
                    <tr>
                        <td colspan="3">SELECT ACTIONS ADD TO GROUPS ABOVE</td>
                    </tr>
                    <tr>
                        <td>@Html.ListBox("listbox", new MultiSelectList(Model.ListActions, "ActionID", "Code"), new { id = "lbxActions", @title = "Unselected actions", style="width:250px" })</td>
                        <td>
                            <input type="button" value=">>" id="ibtnAddActions" title="Add" /><br />
                            <input type="button" value="<<" id="ibtnRemoveActions" title="Remove" /></td>
                        <td>@Html.ListBoxFor(m => m.SelectedActionIDs, new MultiSelectList(Enumerable.Empty<SelectListItem>(), "ActionID", "Code"), new { id = "lbxSelectedActions", @title = "Selected actions", style="width:250px" })</td>
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td>
                <table id="tblMenu"> 
                    <tr>
                        <td colspan="3">SELECT MENU</td>
                    </tr>                   
                    <tr>
                        @{
                        
                            var rootMenu = Model.ListMenu.Where(m => m.ParentID <= 0).OrderBy(t => t.OrderNo).ToList();
                            var lstSelectedIds = (Model.SelMenuIDs == null) ? new List<string>() : Model.SelMenuIDs.Split(';').ToList();
                        }
                        @foreach (var root in rootMenu)
                        {                        
                            <td style="text-wrap: normal; vertical-align: top">
                                @{  string celltext = String.Empty;
                                    var checkRoot = lstSelectedIds.Contains(root.MenuID.ToString());

                                    //Add root
                                    celltext +=
                                         String.Format(
                                         "<input type='checkbox' name='menu{0}' val={0} " + (checkRoot == true ? "checked" : String.Empty) + " id='menu{0}' onclick='checkChild(\"menu{0}\");updateMenu();'/><label for='menu{0}'>{1}</label>&nbsp;&nbsp;<br/>",
                                             root.MenuID, root.Name);
                                    //Add child
                                    var listChild = Model.ListMenu.Where(m => m.ParentID == root.MenuID).OrderBy(t => t.OrderNo).ToList();
                                    foreach (var child in listChild)
                                    {
                                        var checkChild = lstSelectedIds.Contains(child.MenuID.ToString());
                                        celltext += String.Format("&nbsp;|__<input parent='menu{2}' type='checkbox' name='menu{0}' val={0} " + (checkChild == true ? "checked" : String.Empty) + " id='menu{0}' onclick='checkMenu(\"menu{0}\");updateMenu();'/><label for='menu{0}'>{1}</label>&nbsp;&nbsp;<br/>", child.MenuID, child.Name, root.MenuID);
                                        var lstSub = Model.ListMenu.Where(m => m.ParentID == child.MenuID).OrderBy(t => t.OrderNo).ToList();
                                        foreach (var sub in lstSub)
                                        {
                                            var checkSub = lstSelectedIds.Contains(sub.MenuID.ToString());
                                            celltext +=
                                                String.Format(
                                                    "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|__<input parent='menu{2}' type='checkbox' name='menu{0}' val={0} " +
                                                    (checkSub == true ? "checked" : String.Empty) +
                                                    " id='menu{0}' onclick='checkParent(\"menu{0}\");updateMenu();'/><label for='menu{0}'>{1}</label>&nbsp;&nbsp;<br/>",
                                                    sub.MenuID, sub.Name, child.MenuID);
                                        }
                                    }
                                }
                                @Html.Raw(celltext)
                            </td>
                        }
                    </tr>
                </table>
            </td>
        </tr>
        <tr>
            <td>
                <img class="NFButtonLeft NFh" src="/Content/Img/0.png"><input type="submit" class="NFButton NFh" value="Update" title="Click here to add users, actions, menu to selected groups" name="Action" /><img class="NFButtonRight NFh" src="/Content/Img/0.png">
                <img class="NFButtonLeft NFh" src="/Content/Img/0.png"><input type="submit" class="NFButton NFh" value="Recreate" title="Click here to remove current users, actions, menu and add new items  to selected groups" name="Action" /><img class="NFButtonRight NFh" src="/Content/Img/0.png">
            </td>
        </tr>
    </table>
    <input type="hidden" runat="server" id="hidMenu" ref="hidMenu" name="SelMenuIDs" />
}
<script>
    $(document).ready(function () {

        $("#ibtnAddUserGroup").click(function () {


            $("#lbxUserGroups > option:selected").each(function () {
                $(this).remove().appendTo("#lbxSelectedUserGroups");
            });


        });
        $("#ibtnRemoveUserGroup").click(function () {


            $("#lbxSelectedUserGroups > option:selected").each(function () {
                $(this).remove().appendTo("#lbxUserGroups");
            });


        });
        $("#ibtnAddUsers").click(function () {


            $("#lbxUsers > option:selected").each(function () {
                $(this).remove().appendTo("#lbxSelectedUsers");
            });


        });
        $("#ibtnRemoveUsers").click(function () {


            $("#lbxSelectedUsers > option:selected").each(function () {
                $(this).remove().appendTo("#lbxUsers");
            });


        });

        $("#ibtnAddActions").click(function () {


            $("#lbxActions > option:selected").each(function () {
                $(this).remove().appendTo("#lbxSelectedActions");
            });


        });
        $("#ibtnRemoveActions").click(function () {


            $("#lbxSelectedActions > option:selected").each(function () {
                $(this).remove().appendTo("#lbxActions");
            });


        });




    });
    function checkAll(ref) {
        var wrap = $('table[ref="' + ref + '"]');
        wrap.find('input').attr("checked", "checked");
    }
    function uncheckAll(ref) {
        var wrap = $('table[ref="' + ref + '"]');
        wrap.find('input').attr("checked", "");
    }

    function checkChild(menuId) {
        var p = document.getElementById(menuId);
        $('input[parent="' + menuId + '"]').each(function () {
            var id = $(this).attr('id');
            document.getElementById(id).checked = p.checked;
            checkChild(id);
        });


    }
    function checkMenu(menuId) {

        var menu = document.getElementById(menuId);
        //Get all child of this menuid
        $('input[parent="' + menuId + '"]').each(function () {
            var id = $(this).attr('id');
            document.getElementById(id).checked = menu.checked;

        });
        checkParent(menuId);

    }
    function checkParent(menuId) {
        //Get parent of this menu item
        var parentId = $('#' + menuId).attr('parent');
        if (parentId) {
            var parent = document.getElementById(parentId);
            //Get all child of this parent
            var child = $('input[parent="' + parentId + '"]');
            var check = false;
            for (var i = 0; i < child.length; i++) {
                var checkChild = document.getElementById($(child[i]).attr('id'));
                if (checkChild && checkChild.checked == true) {
                    check = true;
                    break;
                }
            }
            //
            parent.checked = check;
            //check parent of parent
            checkParent(parentId);

        }
    }
    function updateMenu() {
        //update Menu
        var table = $('#tblMenu');

        var menu = table.find('input');
        var checkedMenu = "";

        for (var i = 0; i < menu.length; i++) {
            var id = $(menu[i]).attr('id');

            if (document.getElementById(id).checked == true) {
                checkedMenu += (checkedMenu == "" ? "" : ";") + $('#' + id).attr('val');
            }
        }
        $('input[ref="hidMenu"]').val(checkedMenu);

    }
</script>
